package cn.edu.nuc.onlinestore.frame;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.io.SerilizableText;
import cn.edu.nuc.onlinestore.model.Admin;

public class AdminLogin extends JFrame {

	private static final long serialVersionUID = 5976800248183993803L;
	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private JFrame jframe;

	/**
	 * Create the frame.
	 */
	public AdminLogin() {
		jframe = this;
		setTitle("中北线在商场管理系统-管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setIconImage(Toolkit.getDefaultToolkit().getImage("image/gouwu.png"));
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);

		textField = new JTextField();
		textField.setBounds(151, 146, 197, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(151, 185, 197, 21);
		contentPane.add(passwordField);

		JButton button = new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent e) {
				new Admin();                                          //把用户名和密码序列化到文件
				File file= new File("d:/store/admin/lixiaobo.dat");    //
				Admin ad = SerilizableText.deSerializable(file);
				if(textField.getText().equals(ad.getName()) && 
						passwordField.getText().equals(ad.getPass())){
					AdminStore as = new AdminStore();
					as.setVisible(true);		
					as.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); //关闭该窗口不关闭父窗口
					jframe.setVisible(false);                          //不显示父窗口
				}else{
					JOptionPane.showMessageDialog(null, "用户名或密码错误");
				}
			}
		});
		button.setBounds(255, 216, 93, 23);
		contentPane.add(button);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(AdminLogin.class.getResource("/image1/admin.jpg")));
		lblNewLabel.setBounds(208, 54, 86, 82);
		contentPane.add(lblNewLabel);
	}
}
